package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesDepartmentRankReportQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesDepartmentRankReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesDepartmentRankReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesdepartmentrankreport.dto.StatisticalMonthSalesCompanyViewRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesdepartmentrankreport.model.MonthSalesDepartmentRankReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesdepartmentrankreport.service.MonthSalesDepartmentRankReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/2 19:32
 */
@Component
public class MonthSalesDepartmentRankReportQueryRepositoryImpl implements MonthSalesDepartmentRankReportQueryRepository {

    @Resource
    MonthSalesDepartmentRankReportService monthSalesDepartmentRankReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesDepartmentRankReportLoadListVO monthSalesDepartmentRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesDepartmentRankReport>> list = monthSalesDepartmentRankReportService.loadListForPage(monthSalesDepartmentRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesDepartmentBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesDepartmentRankReportLoadListVO monthSalesDepartmentRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesDepartmentRankReport>> list = monthSalesDepartmentRankReportService.loadListForPageSort(monthSalesDepartmentRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> getDepartmentBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesDepartmentRankReportLoadListRequest queryRequest = getListRequestConverter(request);
        Result<List<MonthSalesDepartmentRankReport>> list = monthSalesDepartmentRankReportService.loadList(queryRequest);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesDepartmentRankReportLoadListVO monthSalesDepartmentRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesDepartmentRankReport>> response = monthSalesDepartmentRankReportService.statisticalQuery(monthSalesDepartmentRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalDaySalesQuery(QueryMonthSalesReportRequest request) {
        MonthSalesDepartmentRankReportLoadListVO monthSalesDepartmentRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesDepartmentRankReport>> response = monthSalesDepartmentRankReportService.statisticalDaySalesQuery(monthSalesDepartmentRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalMonthSalesCompanyView(QueryMonthSalesReportRequest request) {
        StatisticalMonthSalesCompanyViewRequest statisticalMonthSalesCompanyViewRequest = StatisticalMonthSalesCompanyViewRequest.builder()
                .statisticalDate(request.getStatisticalDate())
                .planMonth(request.getPlanMonth())
                .dataMark(request.getDataMark())
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesDepartmentId(request.getOrgSalesDepartmentId())
                .bizType(String.valueOf(request.getBizType()))
                .build();
        Result<List<MonthSalesDepartmentRankReport>> response = monthSalesDepartmentRankReportService.statisticalMonthSalesCompanyView(statisticalMonthSalesCompanyViewRequest);
        List<MonthSalesBasicReportSDO> result = Nullable.stream(response.getResult()).map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesDepartmentRankReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesDepartmentRankReportLoadListVO listVO = MonthSalesDepartmentRankReportLoadListVO.newMonthSalesDepartmentRankReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesDepartmentIds(request.getOrgSalesDepartmentIds())
                .channelManageEmployeeIds(request.getChannelManageEmployeeIds())
                .departmentSimpleName(request.getDepartmentSimpleName())
                .departmentManageEmployeeIds(request.getDepartmentManageEmployeeIds())
                .statisticalDate(request.getStatisticalDate())
                .planMonth(request.getPlanMonth())
                .dataMark(request.getDataMark())
                .beginDate(request.getBeginDate())
                .endDate(request.getEndDate())
                .build();
        listVO.setDir(request.getDir());
        listVO.setSortType(request.getSortType());
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesDepartmentRankReportLoadListRequest getListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesDepartmentRankReportLoadListRequest listRequest = MonthSalesDepartmentRankReportLoadListRequest.newMonthSalesDepartmentRankReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .orgSalesDepartmentId(request.getOrgSalesDepartmentId())
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .build();
        return listRequest;
    }
}
